package Tools
{
	import Controls.ItemRender;
	
	import Input.GameInput;
	
	import Native.INativeObject;
	import Native.NativeBitmap;
	
	import Objects.GameObject;
	
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.InteractiveObject;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.geom.ColorTransform;
	
	public class DisplayTool
	{
		public function DisplayTool()
		{
			super();
		}
		public static function moveTo(obj:DisplayObject,x:Number=0,y:Number=0,handleX:Number=0,handleY:Number=0):void
		{
			var rotation:Number=obj.rotation;
			obj.rotation=0;
			var angle:Number =  MathTool.angleToRadian(rotation);
			x-=(Math.cos(angle) * (obj.width * handleX) - Math.sin(angle) * (obj.height * handleY));
			y-=(Math.sin(angle) * (obj.width * handleX) + Math.cos(angle) * (obj.height * handleY));
			obj.x=x;
			obj.y=y;
			obj.rotation=rotation;
		}
		public static function compareIndex(obj1:DisplayObject,obj2:DisplayObject,container:Sprite):int
		{
			if(!obj1.stage||!obj2.stage)return 0;
			
			if(obj1 is DisplayObjectContainer&&DisplayObjectContainer(obj1).contains(obj2)) return 0;
			
			if(!blockMouse(obj2,container))
			{
				return 1;
			}
			
			var obj1DisplayList:Array=getObjDisplayList(obj1,container);
			var obj2DisplayList:Array=getObjDisplayList(obj2,container);
			var length:int=Math.min(obj1DisplayList.length,obj2DisplayList.length);
			var index:int=0;
			while(index<length)
			{
				var disp1:DisplayObject=obj1DisplayList[index];
				var disp2:DisplayObject=obj2DisplayList[index];
				if(disp1!=disp2)
				{
					var index1:int=disp1.parent.getChildIndex(disp1);
					var index2:int=disp2.parent.getChildIndex(disp2);
					if(index1>index2)
					{
						return 1;
					}
					else
					{
						return -1;
					}
				}
				index++;
			}
			
			return 0;
		}
		public static function blockMouse(obj:DisplayObject,container:Sprite):Boolean
		{
			var dispList:Array=getObjDisplayList(obj,container);
			for each(var disp:DisplayObject in dispList)
			{
				if(disp is INativeObject)
				{
					var go:GameObject=INativeObject(disp).go;
					if(!go.checkMouse)
					{
						return false;
					}
					if(go.pixelCheck)
					{
						if(!disp.hitTestPoint(GameInput.i.mousex,GameInput.i.mousey,go.pixelCheck))
						{
							return false;
						}
					}
				}
			}
			return true;
		}
		public static function getObjDisplayList(obj:DisplayObject,container:Sprite):Array
		{
			var list:Array=new Array();
			list.unshift(obj);
			var temp:DisplayObject=obj;
			while(temp!=container)
			{
				if(!temp.parent)
				{
					break;
				}
				list.unshift(temp.parent);
				temp=temp.parent;
			}
			return list;
		}
		public static function tint(obj:DisplayObject,color:uint,strength:Number=1):void
		{
			var colors:Array=MathTool.splitColor(color);
			
			var colorTransform:ColorTransform=new ColorTransform();
			colorTransform.redMultiplier=strength;
			colorTransform.redOffset=colors[0];
			colorTransform.greenMultiplier=strength;
			colorTransform.greenOffset=colors[1];
			colorTransform.blueMultiplier=strength;
			colorTransform.blueOffset=colors[2];
			
			obj.transform.colorTransform=colorTransform;
		}
		public static function bringToFront(obj:DisplayObject):void
		{
			if(obj.parent)
			{
				obj.parent.setChildIndex(obj,obj.parent.numChildren-1);
			}
		}
		public static function bringToBack(obj:DisplayObject):void
		{
			if(obj.parent)
			{
				obj.parent.setChildIndex(obj,0);
			}
		}
		public static function removeObjects(objects:Array):void
		{
			for each(var go:GameObject in objects)
			{
				go.remove();
			}
		}
		public static function createItemRenders(dataList:Array,renderCls:Class,parent:GameObject):Array
		{
			var renderList:Array=new Array();
			
			var index:int=0;
			while(index<dataList.length)
			{
				var itemRender:ItemRender=new renderCls();
				itemRender.name=String(index);
				parent.addChild(itemRender);
				itemRender.data=dataList[index];
				renderList.push(itemRender);
				index++;
			}
			
			return renderList;
		}
		public static function bindItemRenders(dataList:Array,renderList:Array):void
		{
			var index:int=0;
			while(index<renderList.length)
			{
				renderList[index].data=dataList[index];
				index++;
			}
		}
	}
}